<?php


namespace Weskiller\Enum;


use Weskiller\Enum\Contract\Enum;

/**
 * Class UniqueValueEnum
 * @package Weskiller\Enum
 */
class UniqueValueEnum extends Enum
{
    /**
     * UniqueValueEnum constructor.
     * @param $input
     * @throws Exception\InvalidEnumConstantException
     * @throws \ReflectionException
     */
    public function __construct($input)
    {
        if ($value = static::valueOfConstantKey($input)) {
            parent::__construct($input);
        }
        else if ($key = static::keyOfConstantValue($input)) {
            parent::__construct($key);
        }
    }

    /**
     * @param $input
     * @return Enum|null
     * @throws Exception\InvalidEnumConstantException
     * @throws \ReflectionException
     */
    public static function instance($input)
    {
        return new static($input); // TODO: Change the autogenerated stub
    }

    /**
     * @param $input
     * @return Enum|null
     * @throws Exception\InvalidEnumConstantException
     * @throws \ReflectionException
     */
    public static function coerce($input)
    {
        return self::instance($input);
    }

    /**
     * @param $key
     * @param $arguments
     * @return Enum|UniqueValueEnum
     * @throws Exception\InvalidEnumConstantException
     * @throws \ReflectionException
     */
    public static function __callStatic($key, $arguments)
    {
        return self::instance(static::validateConstantKey($key));
    }

    /**
     * @param array $groups
     * @throws \ReflectionException
     */
    public function setGroups(array $groups): void
    {
        foreach ($groups as $group) {
            static::validateConstantValues($group);
        }
        $this->groups = $groups;
    }

    /**
     * @param string $name
     * @param array $value
     * @throws \ReflectionException
     */
    public function addGroup(string $name, array $value) :void
    {
        self::validateConstantValues($value);
        $this->groups[$name] = $value;
    }
}